//
//  EmojiBottomView.swift
//  weiboSwift1.0
//
//  Created by xuNick on 16/5/3.
//  Copyright © 2016年 NN. All rights reserved.
//

import UIKit

enum EmojiType:Int{
    case Recent = 0, Default, Emoji, Lxh
}

class EmojiBottomView: UIStackView {
    
    //当前选中按钮
     var lastSelectButton: UIButton?
    
    //点击要执行的闭包
    var buttonClickCloseSure:((type:EmojiType) -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
        
        distribution = .FillEqually
        //添加四个button
        addchildButton("最近", bgImageNamed: "compose_emotion_table_left", type: .Recent)
        addchildButton("Emoji", bgImageNamed: "compose_emotion_table_mid", type: .Emoji)
        addchildButton("默认", bgImageNamed: "compose_emotion_table_mid", type: .Default)
        addchildButton("浪小花", bgImageNamed: "compose_emotion_table_right", type: .Lxh)
    }
    
    private func addchildButton(title:String, bgImageNamed:String, type:EmojiType){
        let button = UIButton()
        //设置tag
        button.tag = type.rawValue
        // 设置不同状态的背景图片
        button.setBackgroundImage(UIImage(named: "\(bgImageNamed)_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "\(bgImageNamed)_selected"), forState: UIControlState.Selected)
        
        //设置title
        button.setTitle(title, forState: UIControlState.Normal)
        
        // 设置不同状态下的文字颜色
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Selected)

        
        //添加到View上
        addArrangedSubview(button)
        
        //添加监听事件
        button.addTarget(self, action: "clickButton:", forControlEvents: UIControlEvents.TouchUpInside)
        
    }
    
    //MARK: - 点击事件
   
   @objc private func clickButton(button:UIButton){
    
    if lastSelectButton == button {
        return
    }
        button.selected = true
        lastSelectButton?.selected = false
    lastSelectButton = button
    print(button.titleLabel?.text)
    buttonClickCloseSure!(type: EmojiType(rawValue: button.tag)!)
    
    }
    
    
}
